using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

using Janus.Windows.TimeLine;
using Janus.Windows.EditControls;
namespace Janus.NorthwindDemo2007
{
	public partial class TimeLineGroupByDialog
	{

		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);
			NorthwindApp.MainForm.VisualStyleManager1.AddControl(this, true);
		}
		public DialogResult ShowDialog(TimeLine timeLine)
		{
			TimeLineGroup group = null;
			this.FillFieldList(timeLine.Fields, this.cboField0);
			this.FillFieldList(timeLine.Fields, this.cboField1);
			this.FillFieldList(timeLine.Fields, this.cboField2);
			this.FillFieldList(timeLine.Fields, this.cboField3);
			this.cboExpandCollapse.Items.Add("All Expanded", Janus.Windows.TimeLine.GroupMode.Expanded);
			this.cboExpandCollapse.Items.Add("All Collapsed", Janus.Windows.TimeLine.GroupMode.Collapsed);

			this.cboExpandCollapse.SelectedValue = timeLine.GroupMode;
			this.chkRowHeader.Checked = timeLine.ShowLastGroupAsRowHeader;
			if (timeLine.Groups.Count == 0)
			{
				SetGroup(null, true, cboField0, rdbAscending0, rdbDescending0);
			}
			else
			{
				if (timeLine.Groups.Count >= 1)
				{
					group = timeLine.Groups[0];
                    SetGroup(group.Field, (group.SortOrder == Janus.Windows.TimeLine.SortOrder.Ascending), cboField0, rdbAscending0, rdbDescending0);
				}
				if (timeLine.Groups.Count >= 2)
				{
					group = timeLine.Groups[1];
                    SetGroup(group.Field, (group.SortOrder == Janus.Windows.TimeLine.SortOrder.Ascending), cboField1, rdbAscending1, rdbDescending1);
				}
				if (timeLine.Groups.Count >= 3)
				{
					group = timeLine.Groups[2];
					SetGroup(group.Field, (group.SortOrder == Janus.Windows.TimeLine.SortOrder.Ascending), cboField2, rdbAscending2, rdbDescending2);
				}
				if (timeLine.Groups.Count >= 4)
				{
					group = timeLine.Groups[3];
                    SetGroup(group.Field, (group.SortOrder == Janus.Windows.TimeLine.SortOrder.Ascending), cboField3, rdbAscending3, rdbDescending3);
				}
			}
			this.ShowDialog();
			if (this.DialogResult == System.Windows.Forms.DialogResult.OK)
			{
				timeLine.Groups.Clear();
				CreateGroups(timeLine);
			}
			return this.DialogResult;
		}
		private void CreateGroups(TimeLine timeLine)
		{
			int groupsCount = 0;
			if (cboField3.SelectedIndex > 0)
			{
				groupsCount = 4;
			}
			else if (cboField2.SelectedIndex > 0)
			{
				groupsCount = 3;
			}
			else if (cboField1.SelectedIndex > 0)
			{
				groupsCount = 2;
			}
			else if (cboField0.SelectedIndex > 0)
			{
				groupsCount = 1;
			}
			else
			{
				groupsCount = 0;
			}
			TimeLineGroup[] groups = new TimeLineGroup[groupsCount];
			if (groupsCount > 0)
			{
                groups[0] = CreateGroup((TimeLineField)cboField0.SelectedValue, rdbAscending0.Checked);
			}
			if (groupsCount > 1)
			{
                groups[1] = CreateGroup((TimeLineField)cboField1.SelectedValue, rdbAscending1.Checked);
			}
			if (groupsCount > 2)
			{
                groups[2] = CreateGroup((TimeLineField)cboField2.SelectedValue, rdbAscending2.Checked);
			}
			if (groupsCount > 3)
			{
				groups[3] = CreateGroup((TimeLineField)cboField3.SelectedValue, rdbAscending3.Checked);
			}

			timeLine.GroupMode = (GroupMode)this.cboExpandCollapse.SelectedValue;

			timeLine.ShowLastGroupAsRowHeader = this.chkRowHeader.Checked;
			timeLine.Groups.AddRange(groups);
		}
		private TimeLineGroup CreateGroup(TimeLineField field, bool ascending)
		{
			TimeLineGroup group = new TimeLineGroup();
			group.Field = field;
			if (! ascending)
			{
				group.SortOrder = Janus.Windows.TimeLine.SortOrder.Descending;
			}
			return group;
		}

		private void FillFieldList(TimeLineFieldCollection fields, UIComboBox combo)
		{
			combo.Items.Clear();
			combo.Items.Add("(None)", null);
            for (int i = 0; i < fields.Count; i++)
			{
				TimeLineField field = fields[i];
				combo.Items.Add(field.DataMember, field);
			}
		}
		private void SetGroup(TimeLineField field, bool ascending, UIComboBox combo, UIRadioButton optAscending, UIRadioButton optDescending)
		{

			combo.SelectedValue = field;
			if (ascending)
			{
				optAscending.Checked = true;
			}
			else
			{
				optDescending.Checked = true;
			}
		}

		private void cboField0_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (cboField0.SelectedIndex == 0)
			{
				this.rdbAscending0.Enabled = false;
				this.rdbDescending0.Enabled = false;
				cboField1.SelectedIndex = 0;
				cboField1.Enabled = false;
			}
			else
			{
				this.rdbAscending0.Enabled = true;
				this.rdbDescending0.Enabled = true;
				cboField1.Enabled = true;
				if (cboField1.SelectedIndex == -1)
				{
					cboField1.SelectedIndex = 0;
				}
			}
		}

		private void cboField1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (cboField1.SelectedIndex == 0)
			{
				this.rdbAscending1.Enabled = false;
				this.rdbDescending1.Enabled = false;
				cboField2.SelectedIndex = 0;
				cboField2.Enabled = false;
			}
			else
			{
				this.rdbAscending1.Enabled = true;
				this.rdbDescending1.Enabled = true;
				cboField2.Enabled = true;
				if (cboField2.SelectedIndex == -1)
				{
					cboField2.SelectedIndex = 0;
				}
			}
		}

		private void cboField2_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (cboField2.SelectedIndex == 0)
			{
				this.rdbAscending2.Enabled = false;
				this.rdbDescending2.Enabled = false;
				cboField3.SelectedIndex = 0;
				cboField3.Enabled = false;
			}
			else
			{
				this.rdbAscending2.Enabled = true;
				this.rdbDescending2.Enabled = true;
				cboField3.Enabled = true;
				if (cboField3.SelectedIndex == -1)
				{
					cboField3.SelectedIndex = 0;
				}
			}
		}

		private void cboField3_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (cboField3.SelectedIndex == 0)
			{
				this.rdbAscending3.Enabled = false;
				this.rdbDescending3.Enabled = false;
			}
			else
			{
				this.rdbAscending3.Enabled = true;
				this.rdbDescending3.Enabled = true;
			}
		}

		private void btnClear_Click(object sender, System.EventArgs e)
		{
			cboField0.SelectedIndex = 0;
		}

	}
} //end of root namespace